using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Xml;

namespace AliCMS.SystemModule.Bll
{
    public class Privilege : DbObjectBase, IPrivilege
    {
		public Privilege(int instanceID, string lang, bool checkRight, string database)
			: base(instanceID, lang, checkRight, database)
		{
		}

		public override object ExecuteService(HttpContext context)
		{
            object result = null;

            string operation = context.Request.QueryString["Operation"];
            if (operation == "Add")
            {
                Dictionary<string, object> item = new Dictionary<string, object>();
                string value = null;
                value = context.Request.Form["RoleID"];
                if (value != null) item[Alias.Privilege.RoleID] = value;
                item[Alias.Privilege.InstanceID] = context.Request.Form["InstanceID"];
                item[Alias.Privilege.Lang] = context.Request.Form["Lang"];
                item[Alias.Privilege.ObjectID] = context.Request.Form["ObjectID"];
                item[Alias.Privilege.OperationID] = context.Request.Form["OperationID"];
                item[Alias.Privilege.ObjectName] = context.Request.Form["ObjectName"];
                item[Alias.Privilege.OperationName] = context.Request.Form["OperationName"];

                result = Add(item);
            }
            else if (operation == "Modify")
            {
                int id = Convert.ToInt32(context.Request.QueryString["ID"]);

                Dictionary<string, object> changes = new Dictionary<string, object>();
                string value = null;
                value = context.Request.Form["RoleID"];
                if (value != null) changes[Alias.Privilege.RoleID] = value;
                value = context.Request.Form["InstanceID"];
                if (value != null) changes[Alias.Privilege.InstanceID] = value;
                value = context.Request.Form["Lang"];
                if (value != null) changes[Alias.Privilege.Lang] = value;
                value = context.Request.Form["ObjectID"];
                if (value != null) changes[Alias.Privilege.ObjectID] = value;
                value = context.Request.Form["OperationID"];
                if (value != null) changes[Alias.Privilege.OperationID] = value;
                value = context.Request.Form["ObjectName"];
                if (value != null) changes[Alias.Privilege.ObjectName] = value;
                value = context.Request.Form["OperationName"];
                if (value != null) changes[Alias.Privilege.OperationName] = value;

                Modify(id, changes);
            }
            else if (operation == "Delete")
            {
                int id = Convert.ToInt32(context.Request.QueryString["ID"]);

                Delete(id);
            }
            else if (operation == "GetInnerIDFromGuid")
            {
                Guid guid = new Guid(context.Request.QueryString["ID"]);
                result = GetInnerIDFromGuid(guid);
            }
            else if (operation == "List")
            {
                string value = null;

                int page = -1;
                value = context.Request.QueryString["Page"];
                if (value != null) page = Convert.ToInt32(value);

                int pageSize = -1;
                value = context.Request.QueryString["PageSize"];
                if (value != null) pageSize = Convert.ToInt32(value);

                string condition = context.Request.QueryString["Condition"];
                string sort = context.Request.QueryString["Sort"];

                result = List(page, pageSize, condition, sort);
            }
            else
            {
                throw new Exception("lang_header_param");
            }

            return result;
        }

		public int GetInnerIDFromGuid(Guid guid)
        {
            return Dal.GetInnerIDFromGuid(guid);
        }
		public Guid GetGuidFromInnerID(object id)
        {
            return Dal.GetGuidFromInnerID(id);
        }
		public Dictionary<string, object> Get(object id)
        {
            if (CheckRight)
            {
                Guid guid = Dal.GetGuidFromInnerID(id);
                ValidateRight("Get", guid);
            }

            return Dal.Get(id);
        }
		public Guid Add(Dictionary<string, object> item)
		{
            if (CheckRight)
            {
                ValidateRight("Add", Guid.Empty);
            }

            if (!item.ContainsKey(Alias.Privilege.ID) || item[Alias.Privilege.ID].ToString() == String.Empty)
                item[Alias.Privilege.ID] = Guid.NewGuid();
            else if (!(item[Alias.Privilege.ID] is Guid))
                item[Alias.Privilege.ID] = new Guid(item[Alias.Privilege.ID].ToString());

            if (item.ContainsKey(Alias.Privilege.RoleID))
            {
                string roleID = Convert.ToString(item[Alias.Privilege.RoleID]);
                if (String.IsNullOrEmpty(roleID))
                    item[Alias.Privilege.RoleID] = DBNull.Value;
                else
                    item[Alias.Privilege.RoleID] = new Guid(roleID);
            }

            if (!(item[Alias.Privilege.InstanceID] is Guid))
                item[Alias.Privilege.InstanceID] = new Guid(item[Alias.Privilege.InstanceID].ToString());

            if (!(item[Alias.Privilege.ObjectID] is Guid))
                item[Alias.Privilege.ObjectID] = new Guid(item[Alias.Privilege.ObjectID].ToString());

            if (!(item[Alias.Privilege.OperationID] is Guid))
                item[Alias.Privilege.OperationID] = new Guid(item[Alias.Privilege.OperationID].ToString());

            Dal.Add(item);

            Dal.EndWork();
            Manager.UpdatePrivileges();

            return (Guid)item[Alias.Privilege.ID];
        }
		public void Modify(int id, Dictionary<string, object> changes)
		{
            if (CheckRight)
            {
                Guid guid = Dal.GetGuidFromInnerID(id);
                ValidateRight("Modify", guid);
            }

            if (changes.ContainsKey(Alias.Privilege.RoleID))
            {
                string roleID = Convert.ToString(changes[Alias.Privilege.RoleID]);
                if (String.IsNullOrEmpty(roleID))
                    changes[Alias.Privilege.RoleID] = DBNull.Value;
                else
                    changes[Alias.Privilege.RoleID] = new Guid(roleID);
            }

            if (!(changes[Alias.Privilege.InstanceID] is Guid))
                changes[Alias.Privilege.InstanceID] = new Guid(changes[Alias.Privilege.InstanceID].ToString());

            if (!(changes[Alias.Privilege.ObjectID] is Guid))
                changes[Alias.Privilege.ObjectID] = new Guid(changes[Alias.Privilege.ObjectID].ToString());

            if (!(changes[Alias.Privilege.OperationID] is Guid))
                changes[Alias.Privilege.OperationID] = new Guid(changes[Alias.Privilege.OperationID].ToString());

            Dal.Modify(id, changes);

            Dal.EndWork();
            Manager.UpdatePrivileges();
        }
		public void Delete(int id)
		{
            if (CheckRight)
            {
                Guid guid = Dal.GetGuidFromInnerID(id);
                ValidateRight("Delete", guid);
            }

            Dal.Delete(id);

            Dal.EndWork();
            Manager.UpdatePrivileges();
        }
		public DataTable List(int page, int pageSize, string condition, string sort)
        {
            if (CheckRight)
            {
                ValidateRight("List", Guid.Empty);
            }

            return Dal.List(page, pageSize, condition, sort);
        }
		public int GetRecordCount(string condition)
        {
            return Dal.GetRecordCount(condition);
        }

        #region Properties
        private Dal.IPrivilege Dal
        {
            get { return (Dal.IPrivilege)DalBase; }
        }
        #endregion
    }
}
